package com.gylang.crawler.crawler;

import com.alibaba.fastjson.JSON;
import com.gylang.crawler.domain.CrawlerErrorInfo;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.downloader.HttpClientDownloader;

import java.time.LocalDateTime;

/**
 * @author gylang
 * data 2021/5/28
 */
@Component
public class EsLogDownloader extends HttpClientDownloader {


    @Autowired
    private ElasticsearchRestTemplate restTemplate;
    @Override
    protected void onError(Request request) {

        IndexRequest indexRequest = new IndexRequest("crawler_error", "doc");
        CrawlerErrorInfo error = CrawlerErrorInfo.builder()
                .url(request.getUrl())
                .errorMsg("请求失败")
                .errorTime(LocalDateTime.now())
                .type("下载网页")
                .errorType("请求失败")
                .build();
        indexRequest.source(JSON.toJSONString(error), XContentType.JSON);
        super.onError(request);
    }
}
